<template>
  <PageLayout ref="pageLayoutRef">
    <view class="page">
      <div class="header">
        <div class="designer-user">
          <img alt="店铺logo" class="designer-avatar" :src="designerUser.designerInfo?.avatar" />
          <div class="user-profile">
            <h1 class="name">{{ designerUser.designerInfo?.nickname }}</h1>
          </div>
          <button class="like-btn" @click="bindGoProfile()">编辑资料</button>
        </div>
        <p class="profile">{{ designerUser.designerInfo?.introduction || '暂无简介' }}</p>

        <div class="num-tabs">
          <div class="num-tab" @click="bindGo('/subPage/design/sales')">
            <div class="num-count">{{ designerUser.designerInfo?.totalOrders }}</div>
            <div class="num-label">销售量</div>
          </div>
          <div class="num-tab" @click="bindGo('/subPage/design/like')">
            <div class="num-count">{{ designerUser.designerInfo?.designerLikeCount }}</div>
            <div class="num-label">获赞</div>
          </div>
          <div class="num-tab" @click="bindGo('/subPage/design/follower')">
            <div class="num-count">{{ designerUser.designerInfo?.designerFollowCount }}</div>
            <div class="num-label">关注</div>
          </div>
        </div>
      </div>

      <div class="nav-ul">
        <wd-drop-menu :custom-class="tabActive === 0 ? 'custom-class' : ''" style="flex: 1; min-width: 0">
          <wd-drop-menu-item v-model="aiProductOrderBy" :options="option" :title="`个人创作(${workCount || 0})`" @change="handleAiProductOrderByChange" />
        </wd-drop-menu>
        <view :class="{ active: tabActive === 1 }" class="like-filter" @click="bindTab(1)"> 购买作品</view>
      </div>
      <div class="good-body" style="height: 100vh" v-if="tabActive === 0">
        <div class="filter-tags">
          <view :class="{ active: aiTag === 1 }" class="filter-tag" @click="bindAiTag(1)">已上架</view>
          <view :class="{ active: aiTag === 2 }" class="filter-tag" @click="bindAiTag(2)">待上架</view>
          <view :class="{ active: aiTag === 3 }" class="filter-tag" @click="bindAiTag(3)">已删除</view>
        </div>
        <div class="good-list" style="height: calc(100vh - 440rpx)">
          <ScrollList :finished="aiProductIsFinished" :loading="aiProductIsLoading" :showEmpty="aiProductIsFinished && !aiProductList?.length" style="height: calc(100vh - 100rpx); width: calc(100vw - 32rpx)" @load="aiProductLoad" @refresh="aiProductReset">
            <div class="" style="display: flex; flex-direction: column; gap: 16rpx">
              <template v-for="item in aiProductList" :key="item?.id">
                <DesignGoodItem :product="item"></DesignGoodItem>
              </template>
            </div>
          </ScrollList>
        </div>
      </div>
          <un-data class="un-data-content" v-if="tabActive === 1"></un-data>
    </view>
  </PageLayout>
</template>

<script lang="ts" setup>
import { ref, shallowRef } from 'vue';
import ScrollList from '@/components/scroll-list/scroll-list.vue';
import UnData from '@/components/un-data/UnData.vue';
import { onShow } from '@dcloudio/uni-app';
import { getDesignUser, getDesignUserProducts, getProductsCount } from "@/api/designUser";
import { usePagination } from '@/hooks/usePagination';
import DesignGoodItem from '@/components/good/DesignGoodItem.vue';
import PageLayout from '@/components/PageLayout.vue';
import {useRouter} from "uni-mini-router";

const router = useRouter();
const pageLayoutRef = shallowRef(null);
const designerUser = ref({});

const tabActive = ref(0);

const aiProductOrderBy = ref('createTime');
const option = ref<Record<string, any>[]>([
  { label: '上架时间', value: 'createTime' },
  { label: '销售量', value: 'orders' },
  { label: '获赞量', value: 'productsLike' },
]);
const { list: aiProductList, isLoading: aiProductIsLoading, load: aiProductLoad, reset: aiProductReset, isFinished: aiProductIsFinished } = usePagination(queryProducts);
const handleAiProductOrderByChange = (value) => {
  tabActive.value = 0
  aiProductOrderBy.value = value.value;
  aiProductReset();
};

const bindTab = (tab) => {
  tabActive.value = tab;
  // aiProductReset();
};

const aiTag = ref(1);

const bindAiTag = (tag) => {
  aiTag.value = tag;
  aiProductReset();
};

const workCount = ref(0);
onShow(() => {
  aiProductReset();
  queryDesignUser();
  getProductsCount(
    { }
  ).then(res => {
    workCount.value = res.data;
  })
});

function queryProducts(page = 1) {
  const data = {
    page: page,
    type: aiTag.value,
    orderBy: aiProductOrderBy.value,
  };
  return getDesignUserProducts(data);
}

function queryDesignUser() {
  getDesignUser().then((res) => {
    designerUser.value = res.data;
  });
}

const bindGo = (path) => {
  router.push({path});
};

const bindGoProfile = () => {
  router.push({path: '/pages/me/update'});
};
</script>

<style lang="scss" scoped>
.page {
  background: #f3f7f8;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  padding: 32rpx 32px 0;
  background: #ffffff;
}

.designer-user {
  display: flex;
  align-items: center;
  height: 128rpx;

  .designer-avatar {
    width: 80rpx;
    height: 80rpx;
    border-radius: 58rpx 58rpx 58rpx 58rpx;
  }

  .user-profile {
    margin: 0 20px;

    .name {
      height: 38rpx;
      font-weight: 400;
      font-size: 28rpx;
      color: #222222;
      line-height: 38rpx;
    }

    .profile {
      height: 28rpx;
      font-weight: 400;
      font-size: 20rpx;
      color: #6f6f6f;
      line-height: 28rpx;
      margin: 4px 0 0;
    }
  }

  .like-btn {
    height: 48rpx;
    background: #ffffff;
    border-radius: 8rpx 8rpx 8rpx 8rpx;
    border: 2rpx solid #f25a27;
    font-weight: 400;
    font-size: 28rpx;
    color: #f25a27;
    text-align: center;
    line-height: 48rpx;
    padding: 0 8rpx;
    margin-right: 0;

    .active {
      border: 2rpx solid #b7b7b7;
      color: #b7b7b7;
    }
  }
}

.profile {
  font-weight: 400;
  font-size: 28rpx;
  color: #222222;
  line-height: 38rpx;
  text-align: left;
}

.num-tabs {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 128rpx;

  .num-tab {
    .num-count {
      font-weight: bold;
      font-size: 32rpx;
      color: #222222;
      line-height: 42rpx;
      text-align: center;
    }

    .num-label {
      height: 32rpx;
      font-weight: 400;
      font-size: 24rpx;
      color: #222222;
      line-height: 32rpx;
      text-align: center;
    }
  }
}

.nav-ul {
  position: sticky;
  top: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  border-top: 2rpx solid #f2f2f2;
  background: #ffffff;
  z-index: 10;

  .like-filter {
    height: 84rpx;
    line-height: 84rpx;
    text-align: center;

    &.active {
      color: #f25a27;
    }
  }
}

.good-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 16rpx;
  margin: 16rpx;
}

.un-data-content {
  display: flex;
  flex: 1;
  margin-top: 260rpx;
}

.good-body {
  background: #f3f7f8;

  .filter-tags {
    //position: sticky;
    //top: 100rpx;
    display: flex;
    align-items: center;
    gap: 16rpx;
    margin: 16rpx;
    z-index: 9;

    .filter-tag {
      width: 112rpx;
      height: 48rpx;
      background: #ffffff;
      border-radius: 8rpx 8rpx 8rpx 8rpx;
      font-weight: 400;
      font-size: 24rpx;
      color: #222222;
      line-height: 48rpx;
      text-align: center;

      &.active {
        background: #f25a27;
        color: #ffffff;
      }
    }
  }
}
</style>
<style lang="scss">
.nav-ul :deep(.u-dropdown__content__mask) {
  display: none;
}

.custom-class {
  &:deep(.wd-drop-menu__item-title-text) {
    color: #f25a27;
  }

  &:deep(.wd-icon) {
    color: #f25a27;
  }
}
</style>
